/*
 * COMInterop.cs
 * 
 * Microsoft Office SharePoint Server Managed Protocol Handler
 * 
 * Author: John Kozell (johnkoz@microsoft.com)
 *         Microsoft Coroporation
 * 
 * This code is an adaptation from, and interpretation of, the original SPS 2001 SDK
 * and has been updated with available information at the time of writing.
 * Not all the delcarions and alogrithms have been verified and tested
 * and there is no guarantee that they are accurate or valid, now or in the future.
 * 
 * The .NET Managed COM Interop approach contained herin was NOT created in cooperation
 * with the Microsoft SharePoint product group and has not been officially endorced
 * by Microsoft and therefore is not supported. The user of this code assumes any
 * and all risk with its use and should proceed with discression.
 * 
 * THIS CODE AND INFORMATION ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY
 * KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
 * PARTICULAR PURPOSE.
 */

using System;
using System.Runtime.InteropServices;

#region "Windows Data Types"
// http://msdn.microsoft.com/en-us/library/aa383751(VS.85).aspx
using BOOL = System.Int32;     // typedef int BOOL;
using WORD = System.UInt16;    // typedef unsigned short WORD;
using DWORD = System.UInt32;   // typedef unsigned long DWORD;
using SHORT = System.Int16;    // typedef short SHORT;
using USHORT = System.UInt16;  // typedef unsigned short USHORT;
using INT = System.Int32;      // typedef int INT;
using UINT = System.UInt32;    // typedef unsigned int UINT;
using LONG = System.Int32;     // typedef long LONG;
using ULONG = System.UInt32;   // typedef unsigned long ULONG;
using FLOAT = System.Single;   // typedef float FLOAT;
using LPCWSTR = System.String; // typedef CONST WCHAR *LPCWSTR; 
using LPWSTR = System.String;  // typedef WCHAR *LPWSTR;
using LCID = System.UInt32;    // typedef DWORD LCID;

using CLSID = System.Guid;
using VARTYPE = System.UInt16;
using PROPID = System.UInt32;
using DOUBLE = System.Double;
#endregion


public enum SCODE : int
{
	S_OK                        = 0,
	S_FALSE                     = 1,
}


public enum HRESULT : uint
{
	E_NOTIMPL                   = 0x80004001,
	E_FAIL                      = 0x80004005,
	ERROR_INSUFFICIENT_BUFFER   = 0x00000122,
}

// Summary:
//     Represents the number of 100-nanosecond intervals since January 1, 1601.
//     This structure is a 64-bit value.
//     Note: This is pasted from System.Runtime.InteropServices.ComTypes.FILETIME because the typelib compiler complains
//           that it is a Non COM visible value type. So rather than flatten it where used, it is duplicated here.
[StructLayout(LayoutKind.Sequential)]
public struct FILETIME
{
    // Summary:
    //     Specifies the high 32 bits of the FILETIME.
    public DWORD dwHighDateTime;
    //
    // Summary:
    //     Specifies the low 32 bits of the FILETIME.
    public DWORD dwLowDateTime;
}

/// <summary>
/// Summary description for COMInterop.
/// </summary>
public class COMInterop
{
	private COMInterop()
	{
	}


	public static int CopyStringToIntPtr(string str, System.IntPtr intptr)
	{
		string s = str + "\0";
		char[] ca = s.ToCharArray();
		int length = ca.Length;
		Marshal.Copy(ca, 0, intptr, length);

		return length;
	}


    public unsafe static FILETIME DateTimeToFileTime(DateTime dt)
    {
        long lft = dt.ToFileTime();
        FILETIME* pft = (FILETIME*)&lft;
        return *pft;
    }

	public static bool CompareFILETIME(FILETIME ft1, FILETIME ft2)
	{
		if ((ft1.dwHighDateTime == ft2.dwHighDateTime) && (ft1.dwLowDateTime == ft2.dwLowDateTime))
			return true;
		else
			return false;
	}
}
